package com.kykj.kykj.controller;

import com.kykj.kykj.entity.ResultEntity;
import com.kykj.kykj.service.ArticlesNextReplyService;
import com.kykj.kykj.service.impl.ArticlesNextReplyServiceImpl;
import com.kykj.kykj.vo.ArticlesNextReplyVo;
import com.kykj.kykj.vo.ArticlesReplyVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RequestMapping("v2/articles/")
@CrossOrigin//解决跨域问题
@RestController
public class ArticlesNextReplyController {
    @Resource
    ArticlesNextReplyServiceImpl articlesNextReplyService;

    /**
     * 根据回复id获取楼中路回复
     * @param replyId 回复id
     * @return resultEntity
     */
    @GetMapping("getNextReplyList")
    private ResultEntity getNextReplyList(
            @RequestParam("replyId") int replyId
    ){
        ResultEntity resultEntity = new ResultEntity();
        try{
            resultEntity.setData(articlesNextReplyService.getNextReplyList(replyId));
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }

    /**
     * 增加楼中楼回复
     * @param articlesNextReplyVo 楼中楼对象
     * @return resultEntity
     */
    @PostMapping("nextreply")
    private ResultEntity addNextReply(@RequestBody ArticlesNextReplyVo articlesNextReplyVo){
        ResultEntity resultEntity = new ResultEntity();
        try{

            if(articlesNextReplyService.addNextReply(articlesNextReplyVo)>0){
                System.out.println("当前自增的id是"+articlesNextReplyVo.getId()+"------自增");
                resultEntity.setCode(0);
                resultEntity.setMsg("添加成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("添加失败！"+articlesNextReplyVo.getId());
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }

    /**
     * 删除楼中楼
     * @param id 楼中楼id
     * @param userId 用户id
     * @return resultEntity
     */
    @DeleteMapping("nextreply/{id}/{userId}")
    private ResultEntity delNextReply(@PathVariable("id") int id,@PathVariable("userId") int userId){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if(articlesNextReplyService.delNextReply(id,userId)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("删除成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("删除失败！");
            }
        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }

    /**
     * 修改楼中楼回复
     * @param articlesNextReplyVo 楼中楼对象
     * @return resultEntity
     */
    @PutMapping("nextreply")
    private ResultEntity upNextReply(@RequestBody ArticlesNextReplyVo articlesNextReplyVo){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if(articlesNextReplyService.upNextReply(articlesNextReplyVo)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("修改成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("修改失败！");
            }
        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }
}
